package net.hn.hnms.biz.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.process.domain.*;
import net.hn.hnms.biz.process.domain.dto.InspectionCheckDto;
import net.hn.hnms.biz.process.domain.dto.InspectionCheckPoDto;
import net.hn.hnms.biz.process.domain.dto.InspectionConfigCheckDto;
import net.hn.hnms.biz.process.domain.dto.InspectionConfigDto;
import net.hn.hnms.biz.process.domain.vo.InspectionCheckInfoVo;
import net.hn.hnms.biz.process.domain.vo.InspectionCheckVo;
import net.hn.hnms.biz.process.mapper.*;
import net.hn.hnms.biz.process.service.InspectionService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo;
import net.hn.hnms.sys.common.satoken.utils.LoginHelper;
import net.hn.hnms.sys.system.api.RemoteUserService;
import net.hn.hnms.sys.system.api.model.LoginUser;
import net.hn.hnms.sys.system.api.model.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InspectionServiceImpl implements InspectionService {
    // 巡检管理
    @Resource
    private InspectionConfigMapper inspectionConfigMapper;

    // 巡检内容
    @Resource
    private InspectionConfigContentMapper inspectionConfigContentMapper;

    // 巡检计划
    @Resource
    private InspectionCheckMapper inspectionCheckMapper;

    // 巡检结果
    @Resource
    private InspectionCheckResultMapper inspectionCheckResultMapper;

    // 远程调用，获取用户信息
    @Autowired
    private RemoteUserService remoteUserService;

    // 巡检管理-设备管理
    @Resource
    private InspectionDevicMapper inspectionDevicMapper;

    /**
     * 功能描述: 新增巡检管理
     *
     * @param inspectionConfigPo
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 10:42
     */

    @Override
    public Boolean addConfig(InspectionConfigPo inspectionConfigPo) {
        int insert = inspectionConfigMapper.insert(inspectionConfigPo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 批量删除巡检管理
     *
     * @param idList
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 10:50
     */

    @Override
    public Boolean deleteConfigList(List<String> idList) {
        int i = inspectionConfigMapper.deleteBatchIds(idList);
        //同时删除巡检内容
        for (String s : idList) {
            inspectionConfigContentMapper.delete(new LambdaQueryWrapper<InspectionConfigContentPo>()
                    .eq(InspectionConfigContentPo::getInspectionId, s));
            List<InspectionCheckPo> inspectionCheckPos = inspectionCheckMapper.selectList(new LambdaQueryWrapper<InspectionCheckPo>()
                    .eq(InspectionCheckPo::getInspectionId, s));
            for (InspectionCheckPo inspectionCheckPo : inspectionCheckPos) {
                // 如果有巡检计划 就删除巡检计划
                if (inspectionCheckPo != null) {
                    inspectionCheckMapper.delete(new LambdaQueryWrapper<InspectionCheckPo>()
                            .eq(InspectionCheckPo::getInspectionId, s));
                    //同时删除巡检检查结果
                    inspectionCheckResultMapper.delete(new LambdaQueryWrapper<InspectionCheckResultPo>()
                            .eq(InspectionCheckResultPo::getInspectionPlanId, inspectionCheckPo.getInspectionPlanId()));
                }
            }

        }
        // 判断是否有巡检计划
        if (i == idList.size()) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 修改巡检管理
     *
     * @param inspectionConfigPo
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 10:56
     */

    @Override
    public Boolean upDateConfig(InspectionConfigPo inspectionConfigPo) {
        int i = inspectionConfigMapper.updateById(inspectionConfigPo);
        if (i > 1) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 修改巡检管理状态Status
     *
     * @param id
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 11:01
     */
    @Override
    public Boolean chengConfigStatus(String id) {
        InspectionConfigPo inspectionConfigPo = inspectionConfigMapper.selectById(id);
        if (inspectionConfigPo == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该巡检管理ID" + id);
        }
        String inspectionEnabled = inspectionConfigPo.getInspectionEnabled();
        if ("0".equals(inspectionEnabled)) {
            inspectionConfigPo.setInspectionEnabled("1");
            int i = inspectionConfigMapper.updateById(inspectionConfigPo);
            if (i > 0) {
                return true;
            }
        } else if ("1".equals(inspectionEnabled)) {
            inspectionConfigPo.setInspectionEnabled("0");
            int i = inspectionConfigMapper.updateById(inspectionConfigPo);
            if (i > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 功能描述: 分页条件巡检管理
     *
     * @param pageQuery
     * @param inspectionConfigPo
     * @return net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo<net.hn.hnms.biz.process.domain.InspectionConfigPo>
     * @auther: WangLong
     * @date: 2022/8/4 11:18
     */

    @Override
    public TableDataInfo<InspectionConfigPo> pageConfig(PageQuery pageQuery, InspectionConfigPo inspectionConfigPo) {
        Page<InspectionConfigPo> inspectionConfigPoPage = inspectionConfigMapper.selectPage(pageQuery.build(), new LambdaQueryWrapper<InspectionConfigPo>()
                .eq(StringUtils.isNotBlank(inspectionConfigPo.getInspectionName()), InspectionConfigPo::getInspectionName, inspectionConfigPo.getInspectionName())
                .eq(StringUtils.isNotBlank(inspectionConfigPo.getInspectionType()), InspectionConfigPo::getInspectionType, inspectionConfigPo.getInspectionType())
                .eq(StringUtils.isNotBlank(inspectionConfigPo.getInspectionEnabled()), InspectionConfigPo::getInspectionEnabled, inspectionConfigPo.getInspectionEnabled()));

        return TableDataInfo.build(inspectionConfigPoPage);
    }

    /**
     * 功能描述: 添加巡检内容
     *
     * @param inspectionConfigContentPo
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 13:32
     */

    @Override
    public Boolean addConfigContent(List<InspectionConfigContentPo> inspectionConfigContentPo) {
        int insert = 0;
        for (InspectionConfigContentPo configContentPo : inspectionConfigContentPo) {
            InspectionConfigPo inspectionConfigPo = inspectionConfigMapper.selectById(configContentPo.getInspectionId());
            if (inspectionConfigPo == null) {
                throw new HuaNengException(Constants.FAIL, "巡检管理id未查询到,巡检内容添加失败");
            }
            insert += inspectionConfigContentMapper.insert(configContentPo);
        }
        if (insert == inspectionConfigContentPo.size()) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 修改巡检内容
     *
     * @param inspectionConfigContentPo
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 14:06
     */

    @Override
    public Boolean upDateContent(List<InspectionConfigContentPo> inspectionConfigContentPo) {
        int insert = 0;
        for (InspectionConfigContentPo configContentPo : inspectionConfigContentPo) {
            InspectionConfigPo inspectionConfigPo = inspectionConfigMapper.selectById(configContentPo.getInspectionId());
            if (inspectionConfigPo == null) {
                throw new HuaNengException(Constants.FAIL, "巡检管理id未查询到,巡检内容修改失败");
            }
            insert += inspectionConfigContentMapper.updateById(configContentPo);
        }
        if (insert == inspectionConfigContentPo.size()) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: deleteContent
     *
     * @param idList
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 14:09
     */

    @Override
    public Boolean deleteContent(List<String> idList) {
        int i = inspectionConfigContentMapper.deleteBatchIds(idList);
        if (i == idList.size()) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 添加巡检检查计划(下发计划)
     *
     * @param inspectionCheckPoDto
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 14:16
     */

    @Override
    public Boolean addCheck(InspectionCheckPoDto inspectionCheckPoDto) {
        InspectionCheckPo inspectionCheckPo = new InspectionCheckPo();
        BeanUtils.copyProperties(inspectionCheckPoDto, inspectionCheckPo);

        String inspectionId = inspectionCheckPo.getInspectionId();

        InspectionConfigPo inspectionConfigPo = inspectionConfigMapper.selectById(inspectionId);
        if (inspectionConfigPo == null) {
            throw new HuaNengException(Constants.FAIL, "巡检管理id未查询到,巡检检查计划添加失败");
        }
        String inspectionPersonIdsStrng = inspectionCheckPoDto.getInspectionPersonIds();
        //判断巡检人员是否为空
        if (StringUtils.isEmpty(inspectionPersonIdsStrng)) {
            throw new HuaNengException(Constants.FAIL, "巡检检查计划请添加至少一名检查人员");
        }

        // 获取检查计划状态
        String inspectionStatus = inspectionCheckPo.getInspectionStatus();
        //如果巡检检查计划状态未添加 则默认为"0"
        if (StringUtils.isEmpty(inspectionStatus)) {
            inspectionCheckPo.setInspectionStatus("0");
        }
        String[] inspectionPersonIds = inspectionPersonIdsStrng.split(",");
        //巡检人员id
//        for (String inspectionPersonId : inspectionPersonIds) {
//            inspectionCheckPo.setInspectionPersonIds(inspectionCheckPo.getInspectionPersonIds() + inspectionPersonId);
//        }
        inspectionCheckPo.setInspectionPersonIds(inspectionPersonIdsStrng);
        String inspectionPersonNameString = inspectionCheckPoDto.getInspectionPersonNames();
//        String[] inspectionPersonNames = inspectionPersonNameString.split(",");
//        //巡检人员名称
//        for (String inspectionPersonName : inspectionPersonNames) {
//            inspectionCheckPo.setInspectionPersonNames(inspectionCheckPo.getInspectionPersonNames() + inspectionPersonName);
//        }
//        inspectionCheckPo.setInspectionPersonNames(inspectionCheckPo.getInspectionPersonNames());
        inspectionCheckPo.setInspectionPersonNames(inspectionPersonNameString);

        //设置巡检名称
        inspectionCheckPo.setInspectionName(inspectionConfigPo.getInspectionName());
        //设置巡检分类
        inspectionCheckPo.setInspectionType(inspectionConfigPo.getInspectionType());

        //设置巡检设备父ID
        inspectionCheckPo.setDeviceId(inspectionConfigPo.getDeviceId());
        //谁知巡检设备父名称
        inspectionCheckPo.setDeviceName(inspectionConfigPo.getDeviceName());

        //创建检查计划
        int insert = inspectionCheckMapper.insert(inspectionCheckPo);
        //初始化检查结果列表,根据巡检内容列表,生成巡检检查计划结果列表
        List<InspectionConfigContentPo> inspectionConfigContentPoList = inspectionConfigContentMapper.selectList(new LambdaQueryWrapper<InspectionConfigContentPo>()
                .eq(InspectionConfigContentPo::getInspectionId, inspectionConfigPo.getInspectionId()));
        // 遍历内容列表
        int resultCount = 0;
        for (InspectionConfigContentPo inspectionConfigContentPo : inspectionConfigContentPoList) {
            InspectionCheckResultPo inspectionCheckResultPo = new InspectionCheckResultPo();
            //填充巡检计划父id
            inspectionCheckResultPo.setInspectionPlanId(inspectionCheckPo.getInspectionPlanId());
            // 填充巡检内容父id
            inspectionCheckResultPo.setInspectionContentId(inspectionConfigContentPo.getContentId());
            //初始化巡检检查计划完成状态(0未完成,1已完成)

            //巡检设备分类ID
            inspectionCheckResultPo.setDeviceTypeId(inspectionConfigContentPo.getDeviceTypeId());
            //巡检设备分类名称
            inspectionCheckResultPo.setDeviceTypeName(inspectionConfigContentPo.getDeviceTypeName());


            inspectionCheckResultPo.setInspectionResultStatus("0");
            resultCount += inspectionCheckResultMapper.insert(inspectionCheckResultPo);
        }
        if (resultCount != inspectionConfigContentPoList.size()) {
            throw new HuaNengException(Constants.FAIL, "巡检检查计划结果表未正常初始化");
        }
        if (insert > 0) {
            for (String inspectionPersonId : inspectionPersonIds) {
                UserVo user = remoteUserService.getUserById(Long.parseLong(inspectionPersonId));
                if (user == null) {
                    throw new HuaNengException(Constants.FAIL, "未查询到该巡检人的信息:" + inspectionPersonId);
                }
                //获取邮箱，
                String email = user.getEmail();
                //获取电话号码
                String phonenumber = user.getPhonenumber();
                // Todo 获取用户信息,给巡检人员发送信息
            }
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 批量删除巡检管理检查计划
     *
     * @param idList
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 14:49
     */

    @Override
    public Boolean deleteCheck(List<String> idList) {
        int i = inspectionCheckMapper.deleteBatchIds(idList);
        for (String s : idList) {
            inspectionCheckResultMapper.delete(new LambdaQueryWrapper<InspectionCheckResultPo>()
                    .eq(InspectionCheckResultPo::getInspectionPlanId, s));
        }
        if (i == idList.size()) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 更新检查计划结果(完成当前检查计划内容)
     *
     * @param inspectionCheckResultPo
     * @param multipartFile
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/8/4 16:42
     */

    @Override
    public Boolean upDateCheckResult(InspectionCheckResultPo inspectionCheckResultPo, MultipartFile multipartFile) {

        LoginUser loginUserInfo = getLoginUserInfo();
        //获取父检查信息
        InspectionCheckPo inspectionCheckPo1 = inspectionCheckMapper.selectById(inspectionCheckResultPo.getInspectionPlanId());
        //获取巡检人员ID和创建人ID
        String[] split = inspectionCheckPo1.getInspectionPersonIds().split(",");
        Boolean is = false;
        for (String s : split) {
            //如何巡检人id和登录人id是同一个就放行
            if (loginUserInfo.getUserId().toString().equals(s)) {
                is = true;
                break;
            }
        }
        //创建人和登录人是同一个人就放行
        if (loginUserInfo.getUserId().toString().equals(inspectionCheckPo1.getCreateUserId())) {
            is = true;
        }
        if (loginUserInfo.getUserId().toString().equals("1")) {
            is = true;
        }
        if (!is) {
            throw new HuaNengException(Constants.FAIL, "请登录巡检人账号或者发起人账号或者超级管理员账号完成巡检");
        }


        InspectionCheckResultPo inspectionCheckResultPoOne = inspectionCheckResultMapper.selectOne(new LambdaQueryWrapper<InspectionCheckResultPo>()
                .eq(StringUtils.isNotBlank(inspectionCheckResultPo.getInspectionPlanId()), InspectionCheckResultPo::getInspectionPlanId, inspectionCheckResultPo.getInspectionPlanId())
                .eq(StringUtils.isNotBlank(inspectionCheckResultPo.getInspectionContentId()), InspectionCheckResultPo::getInspectionContentId, inspectionCheckResultPo.getInspectionContentId())
                .eq(StringUtils.isNotBlank(inspectionCheckResultPo.getInspectionResultId()), InspectionCheckResultPo::getInspectionResultId, inspectionCheckResultPo.getInspectionResultId()));
        //填充内容
        if (inspectionCheckResultPoOne == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到计划和内容,请输出正确的id");
        }
        inspectionCheckResultPoOne.setInspectionResult(inspectionCheckResultPo.getInspectionResult());
        // TODO 上传文件获取url
        //填充url文件地址
        inspectionCheckResultPoOne.setInspectionResultUrl("");
        // 巡检检查计划完成状态(0未完成,1已完成)
        String inspectionResultStatus = inspectionCheckResultPo.getInspectionResultStatus();
        if (StringUtils.isEmpty(inspectionResultStatus)) {
            inspectionCheckResultPoOne.setInspectionResultStatus("1");
        }
        // TODO 当前登录人员
        // 填充巡检人员编码
        inspectionCheckResultPoOne.setInspectionPersonId("");
        // 填充巡检人员名称
        inspectionCheckResultPoOne.setInspectionPersonName("");

        int i = inspectionCheckResultMapper.updateById(inspectionCheckResultPoOne);

        //判断当前计划内容是否已经全部完成
        //查询当前计划id的所有内容列表
        String inspectionPlanId = inspectionCheckResultPo.getInspectionPlanId();
        List<InspectionCheckResultPo> inspectionCheckResultPos = inspectionCheckResultMapper.selectList(new LambdaQueryWrapper<InspectionCheckResultPo>()
                .eq(InspectionCheckResultPo::getInspectionPlanId, inspectionPlanId)
                .eq(InspectionCheckResultPo::getInspectionResultStatus, "0"));

        //如果没有巡检状态为"0"的集合列表,证明全部都已完成巡检
        if (inspectionCheckResultPos.size() == 0) {
            //将巡检检查计划状态设置为已经完成
            InspectionCheckPo inspectionCheckPo = inspectionCheckMapper.selectById(inspectionPlanId);
            //巡检人员巡检状态,0未完成,1正在巡检,2已完成
            inspectionCheckPo.setInspectionStatus("2");
            inspectionCheckMapper.updateById(inspectionCheckPo);
        }
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 根据巡检管理ID查询巡检内容列表
     *
     * @param id
     * @return java.util.List<net.hn.hnms.biz.process.domain.InspectionConfigContentPo>
     * @auther: WangLong
     * @date: 2022/8/5 10:45
     */

    @Override
    public List<InspectionConfigContentPo> getContentById(String id) {
        List<InspectionConfigContentPo> inspectionConfigContentPoList = inspectionConfigContentMapper.selectList(new LambdaQueryWrapper<InspectionConfigContentPo>()
                .eq(InspectionConfigContentPo::getInspectionId, id));
        if (!inspectionConfigContentPoList.isEmpty()) {
            return inspectionConfigContentPoList;
        }
        return new ArrayList<>();
    }

    /**
     * 功能描述: 查询检查计划列表
     *
     * @param inspectionCheckPo
     * @return java.util.List<net.hn.hnms.biz.process.domain.vo.InspectionCheckVo>
     * @auther: WangLong
     * @date: 2022/8/5 10:57
     */

    @Override
    public List<InspectionCheckVo> getCheckList(InspectionCheckPo inspectionCheckPo) {
        InspectionConfigPo inspectionConfigPo = inspectionConfigMapper.selectById(inspectionCheckPo.getInspectionId());
        if (inspectionConfigPo == null) {
            throw new HuaNengException(Constants.FAIL, "巡检管理ID在数据库中不存在");
        }
        List<InspectionCheckPo> inspectionCheckPos = inspectionCheckMapper.selectList(new LambdaQueryWrapper<InspectionCheckPo>()
                //巡检管理父id
                .eq(StringUtils.isNotBlank(inspectionCheckPo.getInspectionId()), InspectionCheckPo::getInspectionId, inspectionCheckPo.getInspectionId())
                .eq(inspectionCheckPo.getInspectionDate() != null, InspectionCheckPo::getInspectionDate, inspectionCheckPo.getInspectionDate())
                .eq(StringUtils.isNotBlank(inspectionCheckPo.getInspectionStatus()), InspectionCheckPo::getInspectionStatus, inspectionCheckPo.getInspectionStatus())
                .like(StringUtils.isNotBlank(inspectionCheckPo.getInspectionPersonIds()), InspectionCheckPo::getInspectionPersonIds, inspectionCheckPo.getInspectionPersonIds()));
        List<InspectionCheckVo> collect = inspectionCheckPos.stream().map(t -> {
            InspectionCheckVo inspectionCheckVo = new InspectionCheckVo();
            BeanUtils.copyProperties(t, inspectionCheckVo);
            BeanUtils.copyProperties(inspectionConfigPo, inspectionCheckVo);
            return inspectionCheckVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 功能描述: 根据检查计划ID查询巡检结果内容列表
     *
     * @param id
     * @return java.util.List<net.hn.hnms.biz.process.domain.InspectionCheckResultPo>
     * @auther: WangLong
     * @date: 2022/8/5 13:37
     */

    @Override
    public List<InspectionCheckInfoVo> getResultById(String id) {
        InspectionCheckPo inspectionCheckPo = inspectionCheckMapper.selectById(id);
        if (inspectionCheckPo == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到巡检计划");
        }
        List<InspectionCheckResultPo> inspectionCheckResultPoList = inspectionCheckResultMapper.selectList(new LambdaQueryWrapper<InspectionCheckResultPo>()
                .eq(InspectionCheckResultPo::getInspectionPlanId, id));
        if (!inspectionCheckResultPoList.isEmpty()) {
            List<InspectionCheckInfoVo> collect = inspectionCheckResultPoList.stream().map(t -> {
                InspectionCheckInfoVo inspectionCheckInfoVo = new InspectionCheckInfoVo();
                //巡检计划结果的巡检状态
                inspectionCheckInfoVo.setInspectionResultStatus(t.getInspectionResultStatus());
                //查询巡检内容记录
                InspectionConfigContentPo inspectionConfigContentPo = inspectionConfigContentMapper.selectById(t.getInspectionContentId());
                //巡检内容
//                BeanUtils.copyProperties(inspectionConfigContentPo, inspectionCheckInfoVo);
                //内容ID
                inspectionCheckInfoVo.setContentId(inspectionConfigContentPo.getContentId());
                //内容内容
                inspectionCheckInfoVo.setContent(inspectionConfigContentPo.getContent());
                //内容备注
                inspectionCheckInfoVo.setContentRemark(inspectionConfigContentPo.getContentRemark());
                //巡检结果
//                BeanUtils.copyProperties(t, inspectionCheckInfoVo);
                //结果ID
                inspectionCheckInfoVo.setInspectionResultId(t.getInspectionResultId());
                //结果内容
                inspectionCheckInfoVo.setInspectionResult(t.getInspectionResult());
                //结果URL
                inspectionCheckInfoVo.setInspectionResultUrl(t.getInspectionResultUrl());
                //结果状态
                inspectionCheckInfoVo.setInspectionResultStatus(t.getInspectionResultStatus());
                //巡检设备分类ID
                inspectionCheckInfoVo.setDeviceTypeId(t.getDeviceTypeId());
                //巡检设备分类名称
                inspectionCheckInfoVo.setDeviceTypeName(t.getDeviceTypeName());

                return inspectionCheckInfoVo;
            }).collect(Collectors.toList());
            return collect;
        }
        return new ArrayList<>();
    }

    @Override
    public Boolean configAdd(InspectionConfigDto inspectionConfigDto) {

        //获取巡检分类ID
        String inspectionType = inspectionConfigDto.getInspectionType();
        //获取巡检名称
        String inspectionName = inspectionConfigDto.getInspectionName();
        //查询分类和名称是否已经存在
        List<InspectionConfigPo> inspectionConfigPos = inspectionConfigMapper.selectList(new LambdaQueryWrapper<InspectionConfigPo>()
                .eq(InspectionConfigPo::getInspectionType, inspectionType)
                .eq(InspectionConfigPo::getInspectionName, inspectionName));
        if (!inspectionConfigPos.isEmpty()) {
            throw new HuaNengException(Constants.FAIL, "该分类和名称已存在，请输出不同的名称");
        }


        InspectionConfigPo inspectionConfigPo = new InspectionConfigPo();
        //巡检分类
        inspectionConfigPo.setInspectionType(inspectionConfigDto.getInspectionType());
        //巡检名称
        inspectionConfigPo.setInspectionName(inspectionConfigDto.getInspectionName());
        //巡检状态
        inspectionConfigPo.setInspectionEnabled(inspectionConfigDto.getInspectionEnabled());
        //巡检设备父ID
        inspectionConfigPo.setDeviceId(inspectionConfigDto.getDeviceId());
        //巡检设备父name
        InspectionDevicPo inspectionDevicPo = inspectionDevicMapper.selectById(inspectionConfigDto.getDeviceId());
        if (inspectionConfigPo != null) {
            inspectionConfigPo.setDeviceName(inspectionDevicPo.getDeviceName());
        } else {
            inspectionConfigPo.setDeviceName("");
        }
        int insert = inspectionConfigMapper.insert(inspectionConfigPo);
        if (insert > 0) {
            List<InspectionConfigContentPo> listContent = inspectionConfigDto.getListContent();
            if (listContent.isEmpty()) {
                throw new HuaNengException(Constants.FAIL, "巡检内容不能为空");
            }
            int count = 0;
            for (InspectionConfigContentPo inspectionConfigContentPo : listContent) {
                inspectionConfigContentPo.setInspectionId(inspectionConfigPo.getInspectionId());
                count += inspectionConfigContentMapper.insert(inspectionConfigContentPo);
            }
            if (listContent.size() == count) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean configUpdate(InspectionConfigDto inspectionConfigDto) {
        InspectionConfigPo inspectionConfigPo = inspectionConfigMapper.selectById(inspectionConfigDto.getInspectionId());
        if (inspectionConfigPo == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该巡检管理，请检查巡检管理ID是否有错：" + inspectionConfigDto.getInspectionId());
        }
        BeanUtils.copyProperties(inspectionConfigDto, inspectionConfigPo);
        //设置巡检设备父名称
        //巡检设备父name
        InspectionDevicPo inspectionDevicPo = inspectionDevicMapper.selectById(inspectionConfigPo.getDeviceId());
        if (inspectionConfigPo != null) {
            inspectionConfigPo.setDeviceName(inspectionDevicPo.getDeviceName());
        } else {
            inspectionConfigPo.setDeviceName("");
        }

        inspectionConfigMapper.updateById(inspectionConfigPo);
        List<InspectionConfigContentPo> listContent = inspectionConfigDto.getListContent();

        for (InspectionConfigContentPo inspectionConfigContentPo : listContent) {
            String contentId = inspectionConfigContentPo.getContentId();
            InspectionConfigContentPo inspectionConfigContentPo1 = inspectionConfigContentMapper.selectById(contentId);
            if (inspectionConfigContentPo1 == null) {
                inspectionConfigContentPo1 = new InspectionConfigContentPo();
                BeanUtils.copyProperties(inspectionConfigContentPo, inspectionConfigContentPo1);
                inspectionConfigContentPo1.setInspectionId(inspectionConfigDto.getInspectionId());
                inspectionConfigContentMapper.insert(inspectionConfigContentPo1);
            }
            BeanUtils.copyProperties(inspectionConfigContentPo, inspectionConfigContentPo1);
            inspectionConfigContentMapper.updateById(inspectionConfigContentPo1);
        }
        return true;
    }

    @Override
    public ResponsePage<InspectionConfigDto> configPageList(PageQuery pageQuery, InspectionConfigPo inspectionConfigPo) {
        LambdaQueryWrapper<InspectionConfigPo> queryWrapper = new LambdaQueryWrapper<>();
        //巡检类型
        queryWrapper.eq(StringUtils.isNotEmpty(inspectionConfigPo.getInspectionType()), InspectionConfigPo::getInspectionType, inspectionConfigPo.getInspectionType());
        //巡检状态
        queryWrapper.eq(StringUtils.isNotEmpty(inspectionConfigPo.getInspectionEnabled()), InspectionConfigPo::getInspectionEnabled, inspectionConfigPo.getInspectionEnabled());
        //巡检名称
        queryWrapper.eq(StringUtils.isNotEmpty(inspectionConfigPo.getInspectionName()), InspectionConfigPo::getInspectionName, inspectionConfigPo.getInspectionName());
        //煤矿编码
        queryWrapper.eq(StringUtils.isNotBlank(inspectionConfigPo.getMineCode()), InspectionConfigPo::getMineCode, inspectionConfigPo.getMineCode());
        queryWrapper.orderByDesc(InspectionConfigPo::getCreateTime);
        Page<InspectionConfigPo> inspectionConfigPoPage = inspectionConfigMapper.selectPage(pageQuery.build(), queryWrapper);

        return new ResponsePage<>(inspectionConfigPoPage.getRecords(), inspectionConfigPoPage.getTotal());
    }

    @Override
    public ResponsePage<InspectionCheckVo> checkPageList(PageQuery pageQuery, InspectionCheckDto inspectionCheckDto) {
        pageQuery.setPageNum(pageQuery.getPageNum() >= 1 ? (pageQuery.getPageNum() - 1) * pageQuery.getPageSize() : 0);
        List<InspectionConfigCheckDto> list = inspectionCheckMapper.selectPageList(pageQuery.getPageNum(), pageQuery.getPageSize(), inspectionCheckDto);
        if (list.isEmpty()) {
            return new ResponsePage<>(new ArrayList(), 0);
        }
        List<InspectionCheckVo> collect = list.stream().map(t -> {
            InspectionCheckVo inspectionCheckVo = new InspectionCheckVo();
            BeanUtils.copyProperties(t, inspectionCheckVo);
            return inspectionCheckVo;
        }).collect(Collectors.toList());
        return new ResponsePage<InspectionCheckVo>(collect, list.get(0).getTotal());

    }

    /**
     * 修改巡检检查计划
     *
     * @param inspectionCheckPo
     * @return
     */
    @Override
    public Boolean updateCheck(InspectionCheckPo inspectionCheckPo) {
        //获取检查计划id
        String inspectionPlanId = inspectionCheckPo.getInspectionPlanId();
        InspectionCheckPo inspectionCheckPo1 = inspectionCheckMapper.selectById(inspectionPlanId);
        if (inspectionCheckPo1 == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该检查计划ID：" + inspectionPlanId);
        }
        int i = inspectionCheckMapper.updateById(inspectionCheckPo);
        if (i > 0) {
            // TODO 重新通知人员更改
            return true;
        }
        return false;
    }

    /**
     * 批量更新检查结果
     *
     * @param inspectionCheckInfoVoList
     * @return
     */
    @Override
    public Boolean updateResultList(List<InspectionCheckInfoVo> inspectionCheckInfoVoList) {
        if (inspectionCheckInfoVoList.isEmpty()) {
            throw new HuaNengException(Constants.FAIL, "更新列表不可为空");
        }
        //获取登录信息
        LoginUser loginUserInfo = getLoginUserInfo();
        //巡检结果ID
        String inspectionResultId1 = inspectionCheckInfoVoList.get(0).getInspectionResultId();
        InspectionCheckResultPo inspectionCheckResultPo1 = inspectionCheckResultMapper.selectById(inspectionResultId1);
        //获取父id
        String inspectionPlanId1 = inspectionCheckResultPo1.getInspectionPlanId();
        //获取父检查信息
        InspectionCheckPo inspectionCheckPo1 = inspectionCheckMapper.selectById(inspectionPlanId1);
        //获取巡检人员ID和创建人ID
        String[] split = inspectionCheckPo1.getInspectionPersonIds().split(",");
        Boolean is = false;
        for (String s : split) {
            //如何巡检人id和登录人id是同一个就放行
            if (loginUserInfo.getUserId().toString().equals(s)) {
                is = true;
                break;
            }
        }
        //创建人和登录人是同一个人就放行
        if (loginUserInfo.getUserId().toString().equals(inspectionCheckPo1.getCreateUserId())) {
            is = true;
        }
        if (loginUserInfo.getUserId().toString().equals("1")) {
            is = true;
        }
        if (!is) {
            throw new HuaNengException(Constants.FAIL, "请登录巡检人账号或者发起人账号或者超级管理员账号完成巡检");
        }


        String inspectionPlanId = null;
        for (InspectionCheckInfoVo inspectionCheckInfoVo : inspectionCheckInfoVoList) {
            //巡检结果ID
            String inspectionResultId = inspectionCheckInfoVo.getInspectionResultId();
            //巡检结果实体
            InspectionCheckResultPo inspectionCheckResultPo = inspectionCheckResultMapper.selectById(inspectionResultId);
            //内容
            inspectionCheckResultPo.setInspectionResult(inspectionCheckInfoVo.getInspectionResult());
            //url
            inspectionCheckResultPo.setInspectionResultUrl(inspectionCheckInfoVo.getInspectionResultUrl());
            //状态
            inspectionCheckResultPo.setInspectionResultStatus(inspectionCheckInfoVo.getInspectionResultStatus());
            //巡检人员名称
            //巡检人iD
            //获取当前登录人
//            getLoginUserInfo.
            inspectionCheckResultMapper.updateById(inspectionCheckResultPo);
            if (StringUtils.isEmpty(inspectionPlanId)) {
                inspectionPlanId = inspectionCheckResultPo.getInspectionPlanId();
            }
        }

        //判断当前计划内容是否已经全部完成
        //查询当前计划id的所有内容列表
        List<InspectionCheckResultPo> inspectionCheckResultPos = inspectionCheckResultMapper.selectList(new LambdaQueryWrapper<InspectionCheckResultPo>()
                .eq(InspectionCheckResultPo::getInspectionPlanId, inspectionPlanId)
                .eq(InspectionCheckResultPo::getInspectionResultStatus, "0"));

        //如果没有巡检状态为"0"的集合列表,证明全部都已完成巡检
        if (inspectionCheckResultPos.size() == 0) {
            //将巡检检查计划状态设置为已经完成
            InspectionCheckPo inspectionCheckPo = inspectionCheckMapper.selectById(inspectionPlanId);
            //巡检人员巡检状态,0未完成,1正在巡检,2已完成
            inspectionCheckPo.setInspectionStatus("2");
            inspectionCheckMapper.updateById(inspectionCheckPo);
        }

        //巡检结果
        return true;
    }

    /**
     * 我的巡检检查计划
     *
     * @return
     */
    @Override
    public ResponsePage<InspectionCheckVo> myCheckPageList(String mineCode) {
        //获取登录信息
        LoginUser loginUserInfo = getLoginUserInfo();
        //登录人ID
        Long userId = loginUserInfo.getUserId();
        LambdaQueryWrapper<InspectionCheckPo> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询巡检人信息
//        queryWrapper.like(InspectionCheckPo::getInspectionPersonIds, userId);
        //巡检状态为未巡检或者是正在巡检
        queryWrapper.eq(InspectionCheckPo::getInspectionStatus, "0");
        //查询所有的未完成的巡检
        queryWrapper.eq(StringUtils.isNotBlank(mineCode), InspectionCheckPo::getMineCode, mineCode);
        queryWrapper.orderByDesc(InspectionCheckPo::getCreateTime);
        List<InspectionCheckPo> inspectionCheckPos = inspectionCheckMapper.selectList(queryWrapper);


        if (inspectionCheckPos.isEmpty()) {
            return new ResponsePage<>(new ArrayList(), 0);
        }
        //符合条件添加进集合
        List<InspectionCheckPo> list = new ArrayList<>();
        for (InspectionCheckPo inspectionCheckPo : inspectionCheckPos) {
            //遍历人员数组
            String[] split = inspectionCheckPo.getInspectionPersonIds().split(",");
            for (String s : split) {
                //如果当前人员数组有当前登录人
                if (s.equals(userId.toString())) {
                    list.add(inspectionCheckPo);
                    break;
                }
            }
        }
        List<InspectionCheckVo> collect = list.stream().map(t -> {

            InspectionCheckVo inspectionCheckVo = new InspectionCheckVo();
            //巡检计划详情id
            inspectionCheckVo.setInspectionPlanId(t.getInspectionPlanId());
            //巡检时间
            inspectionCheckVo.setInspectionDate(t.getInspectionDate());
            //巡检人员账户编码
            inspectionCheckVo.setInspectionPersonIds(t.getInspectionPersonIds());
            //巡检人员名称
            inspectionCheckVo.setInspectionPersonNames(t.getInspectionPersonNames());
            //巡检状态
            inspectionCheckVo.setInspectionStatus(t.getInspectionStatus());
            //巡检名称
            inspectionCheckVo.setInspectionName(t.getInspectionName());
            //巡检分类
            inspectionCheckVo.setInspectionType(t.getInspectionType());

            //巡检设备父ID
            inspectionCheckVo.setDeviceId(t.getDeviceId());

            //巡检设备父名称
            inspectionCheckVo.setDeviceName(t.getDeviceName());

            return inspectionCheckVo;

        }).collect(Collectors.toList());
        return new ResponsePage<>(collect, collect.size());
    }

    /**
     * 设备管理-新增
     *
     * @param inspectionDevicPo
     * @return
     */
    @Override
    public Boolean deviceAdd(InspectionDevicPo inspectionDevicPo) {
        int insert = inspectionDevicMapper.insert(inspectionDevicPo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 设备管理-批量删除
     *
     * @param idList
     * @return
     */
    @Override
    public Boolean deviceDelete(List<String> idList) {
        int i = inspectionDevicMapper.deleteBatchIds(idList);
        if (i == idList.size()) {
            return true;
        }
        return false;
    }

    /**
     * 设备管理-修改
     *
     * @param inspectionDevicPo
     * @return
     */
    @Override
    public Boolean deviceUpdate(InspectionDevicPo inspectionDevicPo) {
        InspectionDevicPo inspectionDevicPo1 = inspectionDevicMapper.selectById(inspectionDevicPo.getDeviceId());
        if (inspectionDevicPo1 == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该设备ID" + inspectionDevicPo.getDeviceId());
        }
        int i = inspectionDevicMapper.updateById(inspectionDevicPo);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 设备管理-分页查询
     *
     * @return
     */
    @Override
    public ResponsePage<InspectionDevicPo> devicePageList(PageQuery pageQuery, String mineCode) {
        LambdaQueryWrapper<InspectionDevicPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(mineCode), InspectionDevicPo::getMineCode, mineCode);
        lambdaQueryWrapper.orderByDesc(InspectionDevicPo::getCreateTime);
        Page<InspectionDevicPo> inspectionDevicPoPage = inspectionDevicMapper.selectPage(pageQuery.build(), lambdaQueryWrapper);
        if (inspectionDevicPoPage.getRecords().isEmpty()) {
            return new ResponsePage<>(new ArrayList(), 0);
        }
        return new ResponsePage<>(inspectionDevicPoPage.getRecords(), inspectionDevicPoPage.getTotal());
    }

    /**
     * 设备管理-根据ID查询设备详情
     *
     * @param id
     * @return
     */
    @Override
    public InspectionDevicPo deviceById(String id) {
        InspectionDevicPo inspectionDevicPo = inspectionDevicMapper.selectById(id);
        if (inspectionDevicPo == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该ID" + id);
        }
        return inspectionDevicPo;
    }

    private LoginUser getLoginUserInfo() {
        LoginUser loginUser;
        try {
            loginUser = LoginHelper.getLoginUser();
        } catch (Exception e) {
            throw new HuaNengException(Constants.FAIL, "用户未登录,查询不到登录用户信息,无法录入发起人");
        }
        return loginUser;
    }
}
